package com.pj.wisdomlogistics.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.ui.adapter.TakeOutPastaAdapter;

/**
 * Created by HelloWorld on 2017/9/7.
 */

public class CountView extends LinearLayout {
    private ImageView mIvLess;
    private ImageView mIvMore;
    private TextView mTvCount;
    private int currentCount = 0;

    private int maxCount;
    private boolean isPasta;

    public CountView(Context context) {
        this(context , null);
    }

    public CountView(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_count , this,true);


        //inflate(context, R.layout.view_count, null);
        mTvCount = (TextView) findViewById(R.id.tv_dishes_count);
        mIvLess = (ImageView) findViewById(R.id.iv_dishes_less);
        mIvMore = (ImageView) findViewById(R.id.iv_dishes_more);

        mIvMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isPasta){
                    if(currentCount == 0 && TakeOutPastaAdapter.map.size() < 2){
                        mIvLess.setVisibility(VISIBLE);
                        mTvCount.setVisibility(VISIBLE);
                    }else if(currentCount == maxCount){
                        Toast.makeText(context, "达到每天点餐上限，不能再点了哦", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    mIsCanShopListener.isCanShop(currentCount , 1);
                }else{
                    if (currentCount == 0) {
                        mIvLess.setVisibility(VISIBLE);
                        mTvCount.setVisibility(VISIBLE);
                    }else if(currentCount == maxCount){
                        Toast.makeText(context, "熟食每种限点一份", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    currentCount++;
                    mTvCount.setText(currentCount + "");
                    mCountListener.countChange(currentCount);
                }

            }
        });

        mIvLess.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentCount == 1) {
                    mIvLess.setVisibility(GONE);
                    mTvCount.setVisibility(GONE);
                }
                currentCount--;

                if(currentCount == 0 && isPasta){
                    mIsCanShopListener.isCanShop(currentCount , -1);
                }

                mTvCount.setText(currentCount + "");
                mCountListener.countChange(currentCount);
            }
        });
    }

    public void addCount(){
        currentCount++;
        mTvCount.setText(currentCount + "");
        mCountListener.countChange(currentCount);
    }


    public countListener mCountListener;
    public isCanShopListener mIsCanShopListener;

    public interface countListener{
        void countChange(int currentCount);
    }

    public interface isCanShopListener{
        void isCanShop(int currentCount , int type);
    }

    public void setListener(countListener mListener , boolean isPasta){
        this.mCountListener = mListener;
        this.isPasta = isPasta;
    }

    public void setIsCanListener(isCanShopListener mIsCanListener){
        this.mIsCanShopListener = mIsCanListener;
    }

    public void setMaxCount(int count){
        maxCount = count;
    }


}
